import React,{Component} from "react";
import View from "./view.jsx";


// 图像控件
export default class Image extends Component{
  constructor(props) {
    super(props);
    this.state={
      // 图片是否加载好
      error:false
    };
  }
  
  componentDidMount() {
    var img = new Image();
    img.src = this.props.imgSrc;
    img.onload = ()=>this.setState({loading:true});
  }

  render() {
    const {error} = this.state;
    return (
      !error?
        <img alt="{`this.props.alt?this.props.alt:''`}" {...this.props} onError={()=>this.setState({error:true})} />:
        <View {...this.props} style={{width:60,flexDirection:"center",alignItems:"center"}}>加载失败</View>);
  }
}